package com.xxxy.controller;

import com.xxxy.commonResult.CommonResult;
import com.xxxy.dao.ExaminationPaperDao;
import com.xxxy.exception.MyException;
import com.xxxy.log.Log;
import com.xxxy.model.ExaminationPaper;
import com.xxxy.quartz.EndQuartz;
import com.xxxy.quartz.QuartzManager;
import com.xxxy.quartz.StartQuartz;
import com.xxxy.utils.QuartzCronDateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "考试定时任务管理")
@RequestMapping("/examQuartz")
public class ExamQuartzController {

    @Autowired
    QuartzManager quartzManager;

    @Autowired
    ExaminationPaperDao examinationPaperDao;

    @Log(module = "考试定时任务管理", description = "增加定时任务")
    @PutMapping("/addQuartz")
    @ApiOperation(value = "增加定时任务")
    public CommonResult<Boolean> addQuartz(@ApiParam("试卷id") @RequestParam Integer examId) {
        ExaminationPaper examinationPaper = examinationPaperDao.selectById(examId);
        if (examinationPaper != null) {
            try {
                quartzManager.addJob("start" + examId, "start" + examId, "start" + examId, "start" + examId, StartQuartz.class, QuartzCronDateUtils.getCron(examinationPaper.getExamTime()));
                quartzManager.addJob("end" + examId, "end" + examId, "end" + examId, "end" + examId, EndQuartz.class, QuartzCronDateUtils.getCron(examinationPaper.getEndTime()));
                return CommonResult.success();
            } catch (Exception e) {
                e.printStackTrace();
                throw new MyException("增加定时任务失败");
            }
        }
        throw new MyException("未找到改id试卷");
    }

    @Log(module = "考试定时任务管理", description = "更新定时任务")
    @PutMapping("/updateQuartz")
    @ApiOperation(value = "更新定时任务")
    public CommonResult<Boolean> updateQuartz(@ApiParam("试卷id") @RequestParam Integer examId) {
        ExaminationPaper examinationPaper = examinationPaperDao.selectById(examId);
        if (examinationPaper != null) {
            try {
                examinationPaper.setTiming(1);
                examinationPaperDao.updateById(examinationPaper);
                quartzManager.modifyJobTime("start" + examId, "start" + examId, "start" + examId, "start" + examId, QuartzCronDateUtils.getCron(examinationPaper.getExamTime()));
                quartzManager.modifyJobTime("end" + examId, "end" + examId, "end" + examId, "end" + examId, QuartzCronDateUtils.getCron(examinationPaper.getExamTime()));
                return CommonResult.success();
            } catch (Exception e) {
                e.printStackTrace();
                throw new MyException("更新定时任务失败");
            }
        }
        throw new MyException("未找到改id试卷");
    }

    @Log(module = "考试定时任务管理", description = "删除定时任务")
    @PutMapping("/deleteQuartz")
    @ApiOperation(value = "删除定时任务")
    public CommonResult<Boolean> deleteQuartz(@ApiParam("试卷id") @RequestParam Integer examId) {
        ExaminationPaper examinationPaper = examinationPaperDao.selectById(examId);
        if (examinationPaper != null) {
            try {
                examinationPaper.setTiming(0);
                examinationPaperDao.updateById(examinationPaper);
                quartzManager.removeJob("start" + examId, "start" + examId, "start" + examId, "start" + examId);
                quartzManager.removeJob("end" + examId, "end" + examId, "end" + examId, "end" + examId);
                return CommonResult.success();
            } catch (Exception e) {
                e.printStackTrace();
                throw new MyException("删除定时任务失败");
            }
        }
        throw new MyException("未找到改id试卷");
    }

}
